<template>
  <!-- 添加SNMP信息对话框 -->
  <el-dialog 
    :title="formData.id ? '编辑SNMP信息' : '添加SNMP信息'" 
    :visible.sync="dialogVisible" 
    width="500px" 
    @close="handleClose"
  >
    <el-form :model="form" :rules="rules" ref="formRef" label-width="120px" label-position="right">
      <!-- 设备类型 -->
      <el-form-item label="设备类型" prop="type">
        <el-select v-model="form.type" placeholder="请选择设备类型" @change="handleTypeChange">
          <el-option label="服务器" :value="1"></el-option>
          <el-option label="交换机" :value="2"></el-option>
          <el-option label="PDU" :value="3"></el-option>
          <el-option label="播控器" :value="4"></el-option>
          <el-option label="LED" :value="5"></el-option>
          <el-option label="NTP" :value="9"></el-option>
          <el-option label="WiFi" :value="51"></el-option>
          <el-option label="车地无线" :value="52"></el-option>
          <el-option label="车地无线转储" :value="53"></el-option>
          <el-option label="编播设备" :value="54"></el-option>
          <el-option label="网络设备" :value="99"></el-option>
        </el-select>
      </el-form-item>

      <!-- 设备IP -->
      <el-form-item label="设备IP" prop="ip">
        <el-col :span="12">
          <el-input v-model="form.ip" placeholder="请输入设备IP地址" :disabled="!!formData.id" clearable />
        </el-col>
      </el-form-item>

      <!-- 团体名（仅服务器、交换机、PDU、播控器显示） -->
      <el-form-item v-if="[1,2,3,4].includes(form.type)" label="团体名" prop="community">
        <el-col :span="12">
          <el-input v-model="form.community" placeholder="请输入SNMP团体名" clearable />
        </el-col>
      </el-form-item>

      <!-- SNMP版本 -->
      <el-form-item v-if="[1,2,3,4].includes(form.type)" label="SNMP版本" prop="version">
        <el-select v-model="form.version" placeholder="请选择SNMP版本" @change="handleVersionChange">
          <el-option label="v1" :value="0" />
          <el-option label="v2c" :value="1" />
          <el-option label="v3" :value="3" />
        </el-select>
      </el-form-item>

      <!-- 认证密码（仅v3显示） -->
      <el-form-item v-if="form.version === 3" label="认证密码" prop="auth_password">
        <el-input v-model="form.auth_password" placeholder="请输入认证密码" show-password clearable />
      </el-form-item>

      <!-- 加密密码（仅v3显示） -->
      <el-form-item v-if="form.version === 3" label="加密密码" prop="priv_password">
        <el-input v-model="form.priv_password" placeholder="请输入加密密码" show-password clearable />
      </el-form-item>

      <!-- 设备型号 -->
      <el-form-item v-if="[1,2,3,4].includes(form.type)" label="设备型号" prop="model">
        <el-select v-model="form.model" placeholder="请选择设备型号">
          <el-option v-for="model in models" :key="model" :label="model" :value="model"></el-option>
        </el-select>
      </el-form-item>

      <!-- 启用状态 -->
      <el-form-item label="启用状态">
        <el-switch v-model="form.enable" active-text="启用" inactive-text="禁用" />
      </el-form-item>
    </el-form>

    <!-- 对话框底部操作按钮 -->
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import axios from 'axios'

export default {
  props: {
    models: {
      type: Array,
      default: () => []
    },
    visible: {
      type: Boolean,
      default: false
    },
    formData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      dialogVisible: false,
      form: {
        ip: '',
        community: '',
        version: null,
        auth_password: '',
        priv_password: '',
        model: '',
        type: '',
        enable: true
      },
      rules: {
        ip: [
          { required: true, message: '请输入设备IP', trigger: 'blur' },
          {
            pattern: /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/,
            message: '请输入正确的IP地址', trigger: 'blur'
          }
        ],
        community: [
          { 
            validator: (rule, value, callback) => {
              if ([1,2,3,4].includes(this.form.type) && !value) {
                callback(new Error('请输入团体名'))
              } else {
                callback()
              }
            }, 
            trigger: 'blur' 
          }
        ],
        version: [
          { 
            validator: (rule, value, callback) => {
              if ([1,2,3,4].includes(this.form.type) && value === null) {
                callback(new Error('请选择SNMP版本'))
              } else {
                callback()
              }
            }, 
            trigger: 'change' 
          }
        ],
        model: [
          { 
            validator: (rule, value, callback) => {
              if ([1,2,3,4].includes(this.form.type) && !value) {
                callback(new Error('请选择设备型号'))
              } else {
                callback()
              }
            }, 
            trigger: 'change' 
          }
        ],
        type: [
          { required: true, message: '请选择设备类型', trigger: 'change' }
        ]
      }
    }
  },
  watch: {
    visible(val) {
      this.dialogVisible = val
    },
    dialogVisible(val) {
      this.$emit('update:visible', val)
    },
    formData: {
      handler(val) {
        if (val) {
          this.form = { ...val }
        }
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    // 设备类型改变时处理
    handleTypeChange(type) {
      if (![1,2,3,4].includes(type)) {
        this.form.community = '';
        this.form.version = null;
        this.form.model = '';
      } else {
        this.form.version = 1; // 默认版本为 v2c
      }
    },
    // 关闭对话框
    handleClose() {
      this.$refs.formRef.resetFields()
      this.dialogVisible = false
      this.$emit('update:visible', false)
    },
    // 提交表单
    handleSubmit() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          const url = this.form.id ? '/pidscloud/snmp/update' : '/pidscloud/snmp/add'
          const method = this.form.id ? 'put' : 'post'
          
          axios[method](url, this.form)
            .then(() => {
              this.$message.success(this.form.id ? '更新成功' : '添加成功')
              this.$emit('success')
              this.handleClose()
            })
            .catch(error => {
              console.error(this.form.id ? '更新失败' : '添加失败', error)
              this.$message.error(this.form.id ? '更新失败' : '添加失败')
            })
        } else {
          return false
        }
      })
    },
    // SNMP版本改变时处理
    handleVersionChange(version) {
      if (version !== 3) {
        this.form.auth_password = ''
        this.form.priv_password = ''
      }
    }
  }
}
</script>

<style scoped>
.el-form-item {
  margin-bottom: 20px;
}
</style>